package com.thread.sychronized;

/**
 * @author wangyingjie
 * @date
 * @remark
 */
public class AccountDemo extends Thread{

    /**
     * 取钱账户
     */
    private Account account;

    /**
     * 取出金额
     */
    private double money;

    public AccountDemo(Account account,double money){
        super();
        this.account = account;
        this.money = money;
    }

    /**
     * 取钱、扣款进程体
     */
    @Override
    public void run() {
        /*synchronized (account){
            if (account.getBalance()!=0 && account.getBalance()>=money){
                System.out.println("取出【"+money+"】现金");
                //延时操作 模拟线程阻塞
                *//*try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*//*
                double reBalace = account.getBalance()-money;
                account.setBalance(reBalace);
                System.out.println("剩余【"+reBalace+"】现金");
            }else {
                System.out.println("账户余额不足");
            }
        }*/
        try {
            account.draw(money);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        /*Account account = new Account();
        account.setBalance(300);
        AccountDemo t1 = new AccountDemo(account,300);//线程1
        AccountDemo t2 = new AccountDemo(account,200);
        t1.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        t2.start();*/
        Account account = new Account();
        account.setBalance(0.00);
        account.setFlag(false);
        try {
//            account.deposit(200.00);
            account.draw(200.00);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
